home *** CD-ROM | disk | FTP | other *** search
/ CD/PC Actual 76 / DVD Actual 1 Marzo 2003.iso / Trial / TurboCAD 7.1 Pro / Data.Cab / F24178_PrintDr.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-10  |  4.5 KB  |  198 lines

  1. // PrintDr.cpp : Implementation of CPrintDr
  2. #include "stdafx.h"
  3. #include "PrintCAD.h"
  4. #include "PrintDr.h"
  5.  
  6. /////////////////////////////////////////////////////////////////////////////
  7. // CPrintDr
  8.  
  9. COleVariant varMissing((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
  10. COleVariant varTrue(1L);
  11. COleVariant varFalse(0L);
  12.  
  13. #define RC_REQURE    RC_BITBLT | RC_BITMAP64 /*| RC_STRETCHBLT */
  14.  
  15.  
  16. STDMETHODIMP CPrintDr::PrintA(LPDISPATCH WhichDrawing, long pHDC)
  17. {
  18.     AFX_MANAGE_STATE(AfxGetStaticModuleState())
  19.     // TODO: Add your implementation code here
  20.     IDrawing* pIDrawing = NULL;
  21.     pIDrawing = (IDrawing*) WhichDrawing;
  22.     m_sDrawMode = DM_BLACK;
  23.     m_pView = NULL;
  24.     
  25.     if (pIDrawing == NULL)
  26.         return FALSE;
  27.  
  28.  
  29.     HDC hPrintDC = (HDC) pHDC; //dlg.GetPrinterDC();
  30.     if (hPrintDC != NULL)
  31.     {
  32.         int caps = ::GetDeviceCaps(hPrintDC, RASTERCAPS);
  33.  
  34.         if ((caps & (RC_REQURE)) != (RC_REQURE))
  35.         {
  36.             CString cstrMessage;
  37.             cstrMessage.Format("Sorry, can't use your printer.\n \
  38.                                 Printer raster capabilities is %x", caps);
  39.             AfxMessageBox(cstrMessage);
  40.             return FALSE;
  41.         }
  42.  
  43.         BOOL bMonochrom = FALSE;
  44.         // Setup printer DC
  45.         CDC *pPrintDC = new CDC;
  46.         pPrintDC->Attach(hPrintDC);
  47.  
  48.         pPrintDC->m_bPrinting = TRUE;
  49.  
  50.         HDC hScreenIC = CreateIC("Display", NULL, NULL, NULL);
  51.         int logpxScreen = ::GetDeviceCaps(hScreenIC, LOGPIXELSX);
  52.         int logpyScreen = ::GetDeviceCaps(hScreenIC, LOGPIXELSY);
  53.         DeleteDC(hScreenIC);
  54.  
  55.         int iOldMode = pPrintDC->SetMapMode(MM_TEXT);
  56.  
  57.         int logpxPage = pPrintDC->GetDeviceCaps(LOGPIXELSX);
  58.         int logpyPage = pPrintDC->GetDeviceCaps(LOGPIXELSY);
  59.  
  60.         CSize cszOldPage = pPrintDC->ScaleWindowExt(logpxPage, logpxScreen,
  61.                                                     logpyPage, logpyScreen);
  62.  
  63.         int cxPage = pPrintDC->GetDeviceCaps(HORZRES) * 0.9;
  64.         int cyPage = pPrintDC->GetDeviceCaps(VERTRES) * 0.9;
  65.  
  66.         CRect rcPage(0, 0, cxPage, cyPage);
  67.  
  68.         CSize cszOldViewport = pPrintDC->SetViewportExt(cxPage, -cyPage);
  69.  
  70.         if (hPrintDC != NULL)
  71.         {
  72.  
  73.             Views *pViews = NULL;
  74.             View *pView = NULL;
  75.  
  76.             short sOldDrawMode = m_sDrawMode;
  77.  
  78.             HRESULT hRes = S_OK;
  79.  
  80.             DOCINFO docInfo;
  81.             memset(&docInfo, 0, sizeof(DOCINFO));
  82.             docInfo.cbSize = sizeof(DOCINFO);
  83.             docInfo.lpszDocName = "TurboCAD drawing"; //m_strFileName;
  84.  
  85.             pPrintDC->StartDoc(&docInfo);
  86.             pPrintDC->StartPage();
  87.             // ABT 052097
  88.             // According to MS( see mfc\src\viewprnt.cpp)
  89.             // must call OnPrepareDC on newer versions of Windows because
  90.             // StartPage now resets the device attributes.
  91.             ASSERT_VALID(pPrintDC);
  92.             UNUSED(pPrintDC); // unused in release builds
  93.  
  94.             try
  95.             {
  96.                 hRes = pIDrawing->get_Views(&pViews);
  97.                 CHECK_HRESULT(hRes)
  98.  
  99.                 COleVariant varHwnd(varMissing);
  100.                 COleVariant varDC((const long)hPrintDC);
  101.  
  102.                 hRes = pViews->Add(&varHwnd, &varDC, &pView);
  103.                 CHECK_HRESULT(hRes)
  104.  
  105.                 hRes = pView->put_Update(FALSE);
  106.                 CHECK_HRESULT(hRes)
  107.  
  108.                 hRes = pView->put_CenterOnExtents(TRUE);
  109.                 CHECK_HRESULT(hRes)
  110.  
  111.                 hRes = pView->put_MappingMode(MM_TEXT);
  112.                 CHECK_HRESULT(hRes)
  113.  
  114.                 hRes = pView->put_Margins(FALSE);
  115.                 CHECK_HRESULT(hRes)
  116.  
  117.                 hRes = pView->put_FixedAspectRatio(TRUE);
  118.                 CHECK_HRESULT(hRes)
  119.  
  120.                 hRes = pView->put_ScreenLeft(0);
  121.                 CHECK_HRESULT(hRes)
  122.  
  123.                 hRes = pView->put_ScreenTop(0);
  124.                 CHECK_HRESULT(hRes)
  125.  
  126.                 hRes = pView->put_ScreenWidth(cxPage);
  127.                 CHECK_HRESULT(hRes)
  128.  
  129.                 hRes = pView->put_ScreenHeight(cyPage);
  130.                 CHECK_HRESULT(hRes)
  131.  
  132.                 SetDrawModeEx(DM_WHITE | DM_INVERT);
  133.  
  134.                 hRes = pView->ZoomToExtents();
  135.                 CHECK_HRESULT(hRes)
  136.  
  137.             }
  138.             catch (...)
  139.             {
  140.             }
  141.  
  142.             SetDrawModeEx(sOldDrawMode);
  143.  
  144.             if (pView != NULL)
  145.                 pView->Release();
  146.  
  147.             if (pViews != NULL)
  148.                 pViews->Release();
  149.  
  150.             
  151.         }
  152.  
  153.         pPrintDC->EndPage();
  154.         pPrintDC->SetViewportExt(cszOldViewport);
  155.         pPrintDC->SetWindowExt(cszOldPage);
  156.         pPrintDC->SetMapMode(iOldMode);
  157.         int nResult = pPrintDC->EndDoc();
  158.         ASSERT(nResult >= 0);
  159.         pPrintDC->Detach();
  160.  
  161.         delete pPrintDC;
  162.  
  163.     }
  164.  
  165.     // TODO: Add your implementation code here
  166.  
  167.     return S_OK;
  168. }
  169.  
  170.  
  171.  
  172. void CPrintDr::SetDrawModeEx(short nNewValue)
  173. {
  174.     m_sDrawMode = nNewValue;
  175.     ASSERT((m_sDrawMode & DM_CHECKBITMAP) != DM_CHECKBITMAP);
  176.  
  177.     switch (m_sDrawMode)
  178.     {
  179.         case DM_WHITE:
  180.             m_clrBackGround = RGB(255, 255, 255);
  181.         break;
  182.         case DM_INVERT | DM_WHITE:
  183.             m_clrBackGround = RGB(0, 0, 0);
  184.         break;
  185.         case DM_INVERT | DM_BLACK:
  186.             m_clrBackGround = RGB(255, 255, 255);
  187.         break;
  188.         case DM_BLACK:
  189.         default:
  190.             m_clrBackGround = RGB(0, 0, 0);
  191.         break;
  192.     }
  193.  
  194. }
  195.  
  196.  
  197.  
  198.